#pragma once
#include <QGroupBox>
#include <QHostAddress>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QTcpServer>
#include <QTcpSocket>
#include <QWidget>


class Listen : public QWidget
{
    Q_OBJECT

public:
   Listen(QWidget *parent = 0);
   bool isListening;
private:
    QSpinBox *listenPort;
    QPushButton *submitButton;
    QTcpServer *tcpServer;

private slots:
    void onSubmit();
    void incomingConnection();

signals:
    void newConnection(QTcpSocket *connection);

    void startedListening(int port);
    void stoppedListening();

};



class Send : public QWidget
{
    Q_OBJECT
public:
    Send(QWidget *parent = 0);

signals:
    void newConnection(QTcpSocket *tcpConnection);
    void startedSending(QHostAddress addr, int port);
    void stoppedSending();
private slots:
    void onSubmit();
    void connectionReady();
    void errored(QAbstractSocket::SocketError socketError);

private:
    void abortSending();
    void unlockGui();
    void lockGui();


    QLineEdit *sendDestination;
    QSpinBox *sendPort;
    QPushButton *sendConnect;


    //When this tcpSocket is connected, we send it away..
    QTcpSocket *tcpSocket;


};



class CreateConnection : public QWidget
{
    Q_OBJECT

public:
    CreateConnection(QWidget *parent = 0);

signals:
    void newConnection(QTcpSocket *tcpConnection);
    void newStatusBarMsg(QString msg);

private slots:
    void connectedMsg(QTcpSocket *tcpConnection);

    void startedSending(QHostAddress dest, int port);
    void stoppedSending();

    void startedListening(int port);
    void stoppedListening();

private:
    QLabel *connectionTitle;

    Send *send;
    Listen *listen;
};
